//
//  HNChangeNicknameController.m
//  baidaifu
//
//  Created by 朱传正 on 2017/1/3.
//  Copyright © 2017年 haoniu. All rights reserved.
//

#import "HNChangeNicknameController.h"

@interface HNChangeNicknameController ()

@property (nonatomic,strong)NSDictionary *userInfo;
@property (weak, nonatomic) IBOutlet UIView *textView;




@end

@implementation HNChangeNicknameController

-(void)drawView{
     CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
     CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){189.0/255.0,189.0/255.0,189.0/255.0,1});
    _textView.layer.borderColor = color;
    
    _textView.layer.borderWidth = 0.5;
    
}

- (IBAction)changeBtn:(UIButton *)sender {
    if ([AppConfig isNULL:_nickname.text]) {
        [self showHint:@"请输入昵称"];
        return;
    }
    
    
    
    
}

- (IBAction)updateUserInfoAction:(UIButton *)sender {
    
    [self updateUserInfo];
}


#pragma mark - updateUserInfo 更新用户信息
-(void)updateUserInfo{
    if (_nickname.text.length > 12) {
        [self showHint:@"请输入0~12个字符"];
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    [param setObject:_nickname.text forKey:@"nickname"];
    
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_update_userInfo parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"更新用户信息失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            _userInfo = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
            NSLog(@"------obj = %@",_userInfo);
            
            NSString *realName = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"realName"]];
            
            NSString *userImageStr = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"photo"]];
            if ([AppConfig isNULL:userImageStr]) {
                userImageStr = @"null";
            }
            NSString *userNickname = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"nickname"]];
            if ([AppConfig isNULL:userNickname]) {
                
                userNickname = realName;
                
            }
            NSString *userUserName = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"username"]];
            if ([AppConfig isNULL:userUserName]) {
                userImageStr = @"未设置";
            }
            NSDictionary *saveDic = [NSDictionary dictionaryWithObjectsAndKeys:userImageStr,@"photo",userUserName,@"hnLoginName",userNickname,@"nickname", nil];
            
          
            
            [AppConfig saveUserInfo:saveDic];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                HNLoginViewController *loginVC = [[HNLoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"更新用户信息失败"];
    }];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改昵称";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
